package com.jindy.module.goodscollection.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.jindy.common.core.domain.entity.SysDept;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.goodscollection.domain.GoodsCollection;
import com.jindy.module.goodscollection.service.IGoodsCollectionService;
import com.jindy.system.service.ISysDeptService;
import com.jindy.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.common.core.page.TableDataInfo;

/**
 * 物品领用Controller
 *
 * @author ruoyi
 * @date 2022-09-18
 */
@RestController
@RequestMapping("/module/collection")
public class GoodsCollectionController extends BaseController
{
    @Autowired
    private IGoodsCollectionService goodsCollectionService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询物品领用列表
     */
    @PreAuthorize("@ss.hasPermi('module:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsCollection goodsCollection)
    {
        //根据账号获取用户信息
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        //判断用户是否是综合部门
        SysDept sysDept = sysUser.getDept();
        if(sysDept == null || !sysDept.getDeptName().equals("综合部")){
            //不是综合部，则只查询自己的申请记录
            goodsCollection.setUserId(sysUser.getUserId());
        }
        startPage();
        List<GoodsCollection> list = goodsCollectionService.selectGoodsCollectionList(goodsCollection);
        return getDataTable(list);
    }

    /**
     * 导出物品领用列表
     */
    @Log(title = "物品领用", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('module:collection:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsCollection goodsCollection)
    {
        List<GoodsCollection> list = goodsCollectionService.selectGoodsCollectionList(goodsCollection);
        ExcelUtil<GoodsCollection> util = new ExcelUtil<GoodsCollection>(GoodsCollection.class);
        util.exportExcel(response, list, "物品领用数据");
    }

    /**
     * 获取物品领用详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:collection:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(goodsCollectionService.selectGoodsCollectionById(id));
    }

    /**
     * 新增物品领用
     */
    @Log(title = "物品领用", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('module:collection:add')")
    @PostMapping
    public AjaxResult add(@RequestBody GoodsCollection goodsCollection)
    {
        return toAjax(goodsCollectionService.insertGoodsCollection(goodsCollection));
    }

    /**
     * 修改物品领用
     */
    @Log(title = "物品领用", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:collection:update')")
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsCollection goodsCollection)
    {

        return toAjax(goodsCollectionService.updateGoodsCollection(goodsCollection));
    }

    /**
     * 删除物品领用
     */
    @Log(title = "物品领用", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:collection:delete')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(goodsCollectionService.deleteGoodsCollectionByIds(ids));
    }
}
